﻿(function () {
    "use strict";

    var MyNum;
    var BestScore = JSON.parse(window.localStorage.getItem("BestScore"));
    if (BestScore == null) 
        BestScore = {"UserName":"Monkey", "TryTimes":10, "TimeUsed":600};

    var BeginTime;
    var TryTimes;

    document.body.onload = function () {
        document.getElementById("username").innerText = window.localStorage.getItem("UserName");

        var BackButton = document.getElementById("backbutton");
        BackButton.addEventListener("click", Back, false);

        var GuessButton = document.getElementById("guessbutton");
        GuessButton.addEventListener("click", Guess, false);

        document.getElementById("best_username").innerText = BestScore.UserName;
        document.getElementById("best_attempts").innerText = BestScore.TryTimes;
        document.getElementById("best_time").innerText = BestScore.TimeUsed + " seconds";

        NewGame();
    }

    function NewGame() {
        MyNum = parseInt(Math.random() * 100);
        BeginTime = Date.parse(new Date()) / 1000;
        TryTimes = 1;
        document.getElementById("hint").innerText = "";
        document.getElementById("guessvalue").value = "";
    }

    function Back() {
        document.location = "home.html";
    }

    function Guess() {
        var YourNum = document.getElementById("guessvalue").value;
        if (YourNum.length == 0) {
            document.getElementById("hint").innerText = "Please input a number";
        }
        else {
            if (MyNum == parseInt(YourNum)) {
                var UsedTime = Date.parse(new Date()) / 1000 - BeginTime;
                var message = "Congratulations! You've got the correct num after try " + TryTimes + " times in " + UsedTime + " seconds.";
                if (TryTimes < BestScore.TryTimes
                    || TryTimes == BestScore.TryTimes && UsedTime < BestScore.TimeUsed) {
                    message = message + "\nYou set up a new record!";
                    BestScore.UserName = window.localStorage.getItem("UserName");
                    BestScore.TryTimes = TryTimes;
                    BestScore.TimeUsed = UsedTime;
                    window.localStorage.setItem("BestScore", JSON.stringify(BestScore));

                    document.getElementById("best_username").innerText = BestScore.UserName;
                    document.getElementById("best_attempts").innerText = BestScore.TryTimes;
                    document.getElementById("best_time").innerText = BestScore.TimeUsed + " seconds";
                }
                message = message + "\nDo you want to play a new game?"

                ShowMessage(message);
            }
            else {
                if (TryTimes >= 10) {
                    var message = "Sorry that you didn't got the correct num after 10 attempts. \nDo you want to play a new game?"

                    ShowMessage(message);
                }
                else {
                    TryTimes++;
                    if (MyNum > parseInt(YourNum))
                        document.getElementById("hint").innerText = "Please try a bigger number";
                    else
                        document.getElementById("hint").innerText = "Please try a smaller number";
                }
            }
        }
    }

    function ShowMessage(message) {
        var msg = new Windows.UI.Popups.MessageDialog(message);
        msg.commands.append(new Windows.UI.Popups.UICommand("Yes", NewGame));
        msg.commands.append(new Windows.UI.Popups.UICommand("No", Back));
        msg.showAsync();
    }
})();
